package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.wolf2w.cache.service.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.List;

/**
 * redis初始化
 */
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("------初始化---vo-start");

        List<Strategy> sts = strategyService.list();

        for (Strategy strategy : sts) {


            if (strategyStatisRedisService.isStrateVoExists(strategy.getId())) {
                continue;
            }
            StrategyStatisVO vo = new StrategyStatisVO();

            BeanUtils.copyProperties(strategy, vo);

            vo.setStrategyId(strategy.getId());

            strategyStatisRedisService.setStrategyStatisVO(vo);

        }


        System.out.println("------初始化---vo-end");

    }
}
